package content;

import entity.CategoryLocal;
import entity.CategoryLocalHome;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.FinderException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import transfer.CommodityCategory;

/**
 *
 * @author Oleh
 */
public class CategoryContent {

    public static ArrayList getCategories(int count) {
        try {
            Context context = new InitialContext();
            CategoryLocalHome categoryHome = (CategoryLocalHome) context.lookup("java:comp/env/Category");
            Collection collection = categoryHome.findCategories(count);
            Iterator iterator = collection.iterator();
            ArrayList array = new ArrayList();
            while (iterator.hasNext()) {
                array.add(((CategoryLocal) iterator.next()).getCategory());
            }
            return array;
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
    public static CommodityCategory getCategory(Integer idCategory) {
        try {
            Context context = new InitialContext();
            CategoryLocalHome categoryHome = (CategoryLocalHome) context.lookup("java:comp/env/Category");
            CategoryLocal categoryLocal = categoryHome.findByPrimaryKey(idCategory);
            return categoryLocal.getCategory();
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (FinderException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
